<template>
  <el-form
    ref="ruleFormRef"
    :model="lsForm"
    :rules="rules"
    class="demo-ruleForm"
    label-position="top"
    size="small"
  >
    <el-form-item
      :label="t('plc.acknowledgementRequired')"
      prop="acknowledgementRequired"
    >
      <el-switch v-model="lsForm.acknowledgementRequired" />
    </el-form-item>
    <el-form-item
      :label="t('plc.com')"
      prop="payload.com"
      placeholder="组件编号"
    >
      <el-input
        v-model="lsForm.payload.com"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item
      :label="t('plc.car')"
      prop="payload.car"
      placeholder="载体编号"
    >
      <el-input
        v-model="lsForm.payload.car"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item
      :label="t('plc.ct')"
      prop="payload.ct"
      placeholder="控制类型"
    >
      <el-select
        v-model.number="lsForm.payload.ct"
        placeholder="控制类型"
      >
        <el-option
          label="手动"
          value="0"
        />
        <el-option
          label="自动"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.rd')"
      prop="payload.rd"
      placeholder="是否就绪"
    >
      <el-select
        v-model.number="lsForm.payload.rd"
        placeholder="是否就绪"
      >
        <el-option
          label="未就绪"
          value="0"
        />
        <el-option
          label="已就绪"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.lc')"
      prop="payload.lc"
      placeholder="是否有车"
    >
      <el-select
        v-model.number="lsForm.payload.lc"
        placeholder="是否有车"
      >
        <el-option
          label="无车"
          value="0"
        />
        <el-option
          label="有车"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.rs')"
      prop="payload.rs"
      placeholder="运行状态"
    >
      <el-select
        v-model.number="lsForm.payload.rs"
        placeholder="运行状态"
      >
        <el-option
          label="未运行"
          value="0"
        />
        <el-option
          label="已运行"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.la')"
      prop="payload.la"
      placeholder="是否到位"
    >
      <el-select
        v-model.number="lsForm.payload.la"
        placeholder="是否到位"
      >
        <el-option
          label="未到位"
          value="0"
        />
        <el-option
          label="已到位"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.emo')"
      prop="payload.emo"
      placeholder="是否急停"
    >
      <el-select
        v-model.number="lsForm.payload.emo"
        placeholder="是否急停"
      >
        <el-option
          label="未急停"
          value="0"
        />
        <el-option
          label="已急停"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.lg')"
      prop="payload.lg"
      placeholder="是否有货"
    >
      <el-select
        v-model.number="lsForm.payload.lg"
        placeholder="是否有货"
      >
        <el-option
          label="无货"
          value="0"
        />
        <el-option
          label="有货"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.gop')"
      prop="payload.gop"
      placeholder="是否到位"
    >
      <el-select
        v-model.number="lsForm.payload.gop"
        placeholder="是否到位"
      >
        <el-option
          label="未到位"
          value="0"
        />
        <el-option
          label="已到位"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.chs')"
      prop="payload.chs"
      placeholder="运行状态"
    >
      <el-select
        v-model.number="lsForm.payload.chs"
        placeholder="运行状态"
      >
        <el-option
          label="未运行"
          value="0"
        />
        <el-option
          label="运行中"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.lr')"
      prop="payload.lr"
      placeholder="本地/远程"
    >
      <el-select
        v-model.number="lsForm.payload.lr"
        placeholder="本地/远程"
      >
        <el-option
          label="本地"
          value="0"
        />
        <el-option
          label="远程"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.ls')"
      prop="payload.ls"
      placeholder="锁状态"
    >
      <el-select
        v-model.number="lsForm.payload.ls"
        placeholder="锁状态"
      >
        <el-option
          label="未锁"
          value="0"
        />
        <el-option
          label="有锁"
          value="1"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      :label="t('plc.cl')"
      prop="payload.cl"
      placeholder="当前层"
    >
      <el-input
        v-model="lsForm.payload.cl"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!plcServerStatus"
        :loading="loading"
      >
        {{ t('plc.send') }}
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        {{ t('plc.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { nextTick, onMounted, reactive, ref } from 'vue'
import { ElInput, ElMessage, FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { sendMessageByServer } from '@/api/plc'
import { plcStore } from '@/store/plcStore'
import { storeToRefs } from 'pinia'

const store = plcStore()
const loading = ref(false)
const { plcServerStatus } = storeToRefs(store) // 连接状态
const { t } = useI18n()
const ruleFormRef = ref<FormInstance>()

const lsForm = reactive({
  acknowledgementRequired: false,
  payloadType: 'CONV',
  payload: {
    evt: 'LS',
    car: '',
    com: '',
    ct: '',
    rd: '',
    lc: '',
    rs: '',
    la: '',
    emo: '',
    lg: '',
    gop: '',
    chs: '',
    lr: '',
    ls: '',
    cl: ''
  },
})

const rules = reactive<FormRules>({
  'payload.car': [
    {
      required: true,
      message: '请填写载体编号',
      trigger: 'blur',
    },
  ],
  'payload.com': [
    {
      required: true,
      message: '请填写组件编号',
      trigger: 'blur',
    },
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(lsForm)
        .then((res) => {
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'success',
            duration: 1000,
          })
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
